<?php
/* -----------------------------------------------------------------
 *                        Aveghe My Blog
 * -----------------------------------------------------------------
 * Aveghe My Blog: made your blog easy!
 * Copyright (C) 2008 Elia Contini
 * 
 * Aveghe My Blog is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * Aveghe My Blog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

date_default_timezone_set('Europe/Rome');

header("Content-type: application/rss+xml");

require_once 'Aveghe/XmlEntityManager.php';
$xmlEntityManager = new Aveghe_XmlEntityManager();

$title = $xmlEntityManager->toEntityNumber(stripcslashes($this->title));
$domain = ($this->domain).BASE_URL;
$description = $xmlEntityManager->toEntityNumber(stripcslashes($this->description));

require_once 'Aveghe/Info.php';
$avegheInfo = new Aveghe_Info();
$avegheVersion = $avegheInfo->getVersion();

$rss = <<<RSS
<?xml version="1.0" encoding="utf-8"?>
	<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
		<channel>
			<title>$title</title>
			<link>$domain</link>
			<atom:link href="$domain/rss" rel="self" type="application/rss+xml" />
			<description>$description</description>
			<language>it</language>
			<generator>$avegheVersion</generator>
			<category>Magazine</category>
			<docs>$domain/rss</docs>\n\n
RSS;

if(count($this->items) == 0)
	{
		$date = date("D, d M Y H:i:s O");

		$rss .= "
			<item>
				<title>Post not avaible</title>
				<link>$domain</link>
				<guid>$domain</guid>
				<pubDate>$date</pubDate>
				<description>Not avaible</description>
				<category>Not avaible</category>
			</item>\n";
	}
else
	{
		foreach($this->items as $item)
			{
				// TODO - (Aveghe) inserire data
				$rss .= "\t\t\t<item>\n";
				$rss .= "\t\t\t\t<title>".$xmlEntityManager->toEntityNumber(stripcslashes($item["document_name"]))."</title>\n";
				$rss .= "\t\t\t\t<link>".$domain."/post/view/id/".$item['document_id']."</link>\n";
				$rss .= "\t\t\t\t<guid>".$domain."/post/view/id/".$item['document_id']."</guid>\n";
				
				$timestamp = strtotime((string) $item['document_creation_date']);
				$date = date("D, d M Y H:i:s O", $timestamp);
				$rss .= "\t\t\t\t<pubDate>".$date."</pubDate>\n";
				
				$rss .= "\t\t\t\t<description>".$xmlEntityManager->toEntityNumber(stripcslashes($item["document_description"]))."</description>\n";
				$rss .= "\t\t\t\t<category>".$xmlEntityManager->toEntityNumber(stripcslashes($item["document_keywords"]))."</category>\n";
				$rss .= "\t\t\t</item>\n\n";
			}
	}

$rss .= "\t</channel>\n</rss>";

print($rss);
?>